<template>
  <div class="detail-header">
    <router-link tag="div" class="header-abs" to="/" v-show="!showAbs">
      <div class="iconfont header-abs-back">&#xe682;</div>
    </router-link>
    <div class="header-fixed" v-show="showAbs" :style="opacityStyle">
      景点详情
      <router-link to="/">
        <div class="iconfont header-fixed-back">&#xe682;</div>
      </router-link>
    </div>
  </div>
</template>

<script>
export default {
  name: "DetailHeader",
  data() {
    return {
      showAbs: false,
      opacityStyle: {
        opacity: 0
      }
    };
  },
  activated() {
    // 页面展示时，绑定这个事件  但是是全局的
    window.addEventListener("scroll", this.handleScroll);
  },
  deactivated() {
    // 页面即将被替换或隐藏时执行
    // 页面隐藏时进行解绑，否则会污染其他组件
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    handleScroll() {
      // const top = document.documentElement.scrollTop;
      const top =
        document.documentElement.scrollTop ||
        window.pageYOfset ||
        document.body.scrollTop;
      console.log(top);
      if (top > 30) {
        let opacity = top / 140;
        opacity = opacity > 1 ? 1 : opacity;
        this.opacityStyle = {
          opacity
        };
        this.showAbs = true;
      } else {
        this.showAbs = false;
      }
    }
  }
};
</script>

<style lang="stylus" scoped>
@import '~@/assets/styles/varibles.styl'

.detail-header {
  position: relative
  z-index: 9
  .header-abs {
    position: absolute
    left: 0.2rem
    top: 0.2rem
    width: 0.72rem
    height: 0.72rem
    line-height: 0.72rem
    border-radius: 0.4rem
    text-align: center
    background: rgba(0, 0, 0, 0.5)
    .header-abs-back {
      font-size: 0.5rem
      color: #fff
    }
  }
}
.header-fixed {
  height: $headerHeight
  line-height: $headerHeight
  text-align: center
  overflow: hidden
  color: #fff
  background: $bgColor
  font-size: 0.32rem
  position: fixed
  top: 0
  left: 0
  right: 0
  .header-fixed-back {
    width: 0.64rem
    text-align: center
    font-size: 0.4rem
    color: #fff
    position: absolute
    top: 0
    left: 0
  }
}
</style>
